package homework;

import java.util.List;

class ListNode {
    int val;
    ListNode prev;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}

public class DoubleLinkedList {
    ListNode dummyHead;
    ListNode last;

    public DoubleLinkedList() {
        this.dummyHead = new ListNode(-1);
    }

    //找到某一位置的结点
    public ListNode findIndex(int index) {
        ListNode cur = dummyHead.next;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (this.dummyHead.next == null) {
            node.prev = this.dummyHead;
            this.dummyHead.next = node;
            this.last = node;
            return;
        } else {
            node.next = this.dummyHead.next;
            node.prev = this.dummyHead;
            this.dummyHead.next.prev = node;
            this.dummyHead.next = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (this.dummyHead.next == null && this.last == null) {
            node.prev = this.dummyHead;
            this.dummyHead.next = node;
            this.last = node;
            return;
        } else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    //任意位置插入，第一个数据结点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("输入的位置不合法！");
            return;
        }else {
            if(index == 0){
                addFirst(data);
                return;
            }

            if(index == size()){
                addLast(data);
            }else {
                ListNode node = new ListNode(data);
                ListNode cur;
                cur = findIndex(index);
                cur.prev.next = cur.prev;
                cur.prev = node;
                node.next = cur;
            }
        }
    }

    //查找是否包含关键字key
    public boolean contains(int key){
        ListNode cur = this.dummyHead.next;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key){
        ListNode cur = this.dummyHead.next;

        while (cur != null){
            if(cur.val == key){
                if(cur == this.dummyHead.next){
                    this.dummyHead.next = this.dummyHead.next.next;
                    this.dummyHead.next.prev = this.dummyHead;
                }else {
                    cur.prev.next = cur.next;

                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        this.last = cur.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }

    public void removeAllKey(int key){
        ListNode cur = dummyHead.next;
        while (cur != null){
            if(cur.val == key){
                if(cur == this.dummyHead.next){
                    this.dummyHead.next = this.dummyHead.next.next;
                    this.dummyHead.next.prev = null;
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        this.last = cur.prev;
                    }else{
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    private int size() {
        ListNode cur = this.dummyHead.next;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = this.dummyHead.next;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList =new DoubleLinkedList();
        doubleLinkedList.addFirst(1);
        doubleLinkedList.addFirst(2);
        doubleLinkedList.addFirst(3);
        doubleLinkedList.addFirst(0);
        System.out.println(doubleLinkedList.size());
        doubleLinkedList.display();
        System.out.println(doubleLinkedList.contains(0));
        System.out.println("======================================");
        doubleLinkedList.removeAllKey(1);
        doubleLinkedList.display();
        doubleLinkedList.addFirst(0);
        doubleLinkedList.display();
    }
}

